<template>
  <div class="mod-config">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClickTab">
      <el-tab-pane label="字体管理" name="1">
        <el-button type="primary" @click="openUnifyDialog('字体')">添加字体</el-button>
        <el-button type="danger" v-show="showMoveBtn" @click="multipledeleteHandle">批量删除</el-button>
        <el-table :data="dataList" stripe v-loading="dataListLoading" style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column prop="" type="selection" />
          <el-table-column prop="id" label="字体id" show-overflow-tooltip></el-table-column>
          <el-table-column prop="characterType" label="字体名称"> </el-table-column>
          <el-table-column prop="updatetime" label="更新时间"> </el-table-column>
          <el-table-column prop="order" label="顺序"> </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="modifyHanle(scope.row)">修改</el-button>
              </el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页条 -->
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane label="书家管理" name="2">
        <el-button type="primary" @click="openUnifyDialog()">添加书家</el-button>
        <el-button type="danger" v-show="showMoveBtn" @click="multipledeleteHandle">批量删除</el-button>
        <el-table :data="dataList" stripe v-loading="dataListLoading" style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column prop="" type="selection" />
          <el-table-column prop="id" label="书家id" show-overflow-tooltip></el-table-column>
          <el-table-column prop="name" label="书家名称"> </el-table-column>
          <el-table-column prop="updatetime" label="更新时间"> </el-table-column>
          <el-table-column prop="order" label="顺序"> </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="modifyHanle(scope.row)">修改</el-button>
              </el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页条 -->
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
    <!-- 新增字体、书法、模态框 -->
    <el-dialog :title=isStatusName+tabName :visible.sync="dialogUnifyVisible" width="600px" destroy-on-close>
      <el-form ref="unifyForm" :model="DataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px"
        :rules="rules">
        <el-form-item :label="`${tabName}名称`" prop="name">
          <el-input v-model="DataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="mobile">
          <el-input-number v-model="DataForm.order" :min="1" :max="100" :precision="0" label="描述文字">
          </el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd()">取 消</el-button>
        <el-button type="primary" @click="doAdd()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 字体
        activeName: '1',
        tabName: '字体',
        tabPosition: 'left',
        tab: 1,
        dataList: null,
        dialogUnifyVisible: false,
        DataForm: {
          id: null,
          name: '',
          type: '',
          name: '',
          order: 1,
        },
        isAdd: true,
        tag: 'years',
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        showMoveBtn: false,
        dataListLoading: false,
        rules: {
          name: [{
            required: true,
            message: '请输入内容',
            trigger: 'change'
          }, {
            min: 1,
            max: 20,
            message: '长度在 1 到 20 个字符',
            trigger: 'change'
          }]
        },
      };
    },
    components: {},
    created() {
      this.getDataListById()
    },
    watch: {
      // 当tab切换时，改变
      tab: {
        handler(newValue, OldValue) {
          switch (newValue) {
            case '1':
              this.tabName = '字体'
              this.tag = 'years'
              break;
            case '2':
              this.tabName = '书家'
              this.tag = 'style'
              break;
            case '3':
              this.tabName = '书家'
              this.tag = 'calligrapher'
              break;
            default:
              this.tabName = '字体'
              this.tag = 'years'
          }
          this.multipleSelection = []
        }
      }
    },
    computed: {
      isStatusName: function() {
        if (this.isAdd) {
          return '新增'
        } else {
          return '修改'
        }
      }
    },
    methods: {
      // 点击切换选项卡
      handleClickTab(tab, event) {
        this.tab = tab.name
        this.pageIndex = 1
        this.getDataListById()
      },
      // 根据选项卡id获取列表
      getDataListById() {

        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl("/sf/copybookCharacter/cc/list"),
          method: "get",
          params: this.$http.adornParams({
            type: this.tab,
            curPage: this.pageIndex,
            pageSize: this.pageSize,
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.data.record;
            this.totalPage = data.data.total;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 新增 -- 打开字体书法框
      openUnifyDialog() {
        this.dialogUnifyVisible = true
        this.isAdd = true
        this.DataForm = {
          id: null,
          type:this.tab,
          name: '',
          order: 1
        }
      },
      // 字体书法框--取消按钮
      cancelAdd() {
        this.dialogUnifyVisible = false
        this.DataForm = {
          id: null,
          name: '',
          type:this.tab,
          order: 1
        }
      },
      // 字体书法框--确定按钮
      doAdd() {
        this.$refs['unifyForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sf/copybookCharacter/cc/${this.isAdd?'save':'update'}`),
              method: "post",
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              data: this.$http.adornData(this.DataForm, true, 'form')
            }).then(({
              data
            }) => {
              if (data && data.code === 0 && data.success) {
                this.$message({
                  message: `${this.isAdd?'新增成功':'更新成功'}`,
                  type: "success",
                  duration: 1000,
                  onClose: () => {
                    this.dialogUnifyVisible = false
                    this.getDataListById()
                  },
                });
              } else {
                this.$message.error(data.message);
              }
            });
          } else {
            this.dataListLoading = false
            console.log('error submit!!')
            return false
          }
        })


      },
      // 修改--打开书法框
      modifyHanle(row) {
        this.dialogUnifyVisible = true
        this.isAdd = false
        this.DataForm = {
          id: row.id,
          name: row.characterType||row.name,
          type:this.tab,
          order: row.order,
        }
      },

      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataListById()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataListById()
      },
      // 删除
      deleteHandle(row) {
        this.$confirm(`确定对id[${row.id}]进行[删除]操作?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            this.dataListLoading = true
            this.$http({
              url: this.$http.adornUrl("/sf/copybookCharacter/cc/delete"),
              method: "post",
              data: this.$http.adornData([row.id], false),
            }).then(({
              data
            }) => {
              if (data && data.code === 0 && data.success) {
                this.$message({
                  message: "删除成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataListById()
                  },
                });
              } else {
                this.dataListLoading = false
                this.$message.error(data.message);
              }
            });
          })
          .catch(() => {});
      },
      // 删除多个文件
      multipledeleteHandle() {
        const ids = []
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        this.$confirm(`确定对这些进行[删除]操作?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl("/sf/copybookCharacter/cc/delete"),
              method: "post",
              data: this.$http.adornData(ids, false),
            }).then(({
              data
            }) => {
              if (data && data.code === 0 && data.success) {
                this.$message({
                  message: "删除成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataListById()
                  },
                });
              } else {
                this.getDataListById()
                this.$message.error(data.message);
              }
            });
          })
          .catch(() => {});
      },
      // 点击多选框事件
      handleSelectionChange(val) {
        this.showMoveBtn = val.length != 0
        this.multipleSelection = val
      },
    },
  };
</script>
<style lang="less" scoped>
  .filename-span {
    vertical-align: middle;
    margin: 0 2px;
  }

  .isDir {
    cursor: pointer;
    line-height: 30px;

    &:hover {
      text-decoration: underline;
    }
  }

  .el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
    width: 100%;

    .spanDir {
      width: 100%;
      color: #06a7ff;
    }
  }

  .spanDir {
    width: 100%;
  }

  .el-tree-node__content {
    width: 100%;

    &:hover {
      .spanDir {
        width: 100%;
        color: #06a7ff;
      }
    }
  }
</style>
